package com.example.tanlee.ocrgraduation.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tanlee.ocrgraduation.R;
import com.example.tanlee.ocrgraduation.application.MyApplication;
import com.example.tanlee.ocrgraduation.bean.PictureEntity;

import java.util.ArrayList;

/**
 * 主要用于现在主界面上的图片信息
 * Created by Tanlee on 2017/1/19.
 */
public class PictureAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<PictureEntity> pictureEntities;

    public PictureAdapter() {
        super();
    }

    public PictureAdapter(Context context, ArrayList<PictureEntity> pictureEntities) {
        this.context = context;
        this.pictureEntities = pictureEntities;
    }

    @Override
    public int getCount() {
        if (pictureEntities != null){
            return pictureEntities.size();
        }
        else{
            return 0;
        }
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setPictureEntities(ArrayList<PictureEntity> pictureEntities) {
        this.pictureEntities = pictureEntities;
    }

    @Override
    public Object getItem(int position) {
        if(pictureEntities != null) {
            return pictureEntities.get(position);
        }
        else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        if (pictureEntities != null){
            return position;
        }
        else{
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_picture_list, null);
            viewHolder = new ViewHolder();
            findViewById(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        initViewInfo(viewHolder,pictureEntities.get(position));
        return convertView;
    }

    /**
     * 初始化应用程序
     * @param viewHolder
     * @param pictureEntity
     */
    private void initViewInfo(ViewHolder viewHolder,PictureEntity pictureEntity){
        viewHolder.txtProcessTime.setText(pictureEntity.getProcessTime());
        viewHolder.txtName.setText(pictureEntity.getUserName());
        viewHolder.txtResult.setText(pictureEntity.getProcessResult());
        viewHolder.txtRecognitionRate.setText(pictureEntity.getRecognitionRate());
        viewHolder.txtDate.setText(pictureEntity.getDate().toString());
    }

    private void findViewById(ViewHolder viewHolder,View convertView){
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txt_picture_list_name);
        viewHolder.headerImg = (ImageView) convertView.findViewById(R.id.iv_picture_list_header);
        viewHolder.ivThumbnailImg = (ImageView) convertView.findViewById(R.id.img_picture_list);
        viewHolder.txtDate = (TextView) convertView.findViewById(R.id.txt_picture_time);
        viewHolder.txtResult = (TextView) convertView.findViewById(R.id.txt_pictures_process_result);
        viewHolder.txtRecognitionRate = (TextView) convertView.findViewById(R.id.txt_picture_success_rate);
        viewHolder.txtProcessTime = (TextView) convertView.findViewById(R.id.txt_picture_time);
    }

    static class ViewHolder{
        ImageView headerImg; // 头像图片
        ImageView ivThumbnailImg; // 缩略图
        TextView txtName;  // 用户名
        TextView txtDate;  // 拍摄时间
        TextView txtProcessTime;   // 处理时间
        TextView txtRecognitionRate;  // 识别成功率
        TextView txtResult;  // 处理结果
    }

}
